<%@page import="java.util.ArrayList"
import="data.Rmi"
import="bean.Patient"
import="bean.Ordonnance"
import="bean.Medecin"

%>

<jsp:useBean id="manager" scope="session" class="manager.Manager"></jsp:useBean>

<%
	int debutRecherche = 0;
	int nbRecherche = 5;
	int numPage = 0;
	boolean pagePrecedente = false;
	boolean pageSuivant = false;
	
	boolean error = false;
	String nom = "";
	String prenom = "";
	Patient patient =null;
	
	if(request.getParameter("listerOrdonnance") != null){
		int idPatient = Integer.parseInt(request.getParameter("idPatient"));
		patient = manager.getListPatient().get(idPatient);
		manager.setListOrdonnance(Rmi.rechercheOrdonnance(patient.getId(),debutRecherche,nbRecherche));
		if(manager.getListOrdonnance().size()>=nbRecherche) pageSuivant = true;
		
	}else if(request.getParameter("changePage") != null){
		
		int numpage = Integer.parseInt(request.getParameter("numPage"));
		numPage = numpage;
		int idPatient = Integer.parseInt(request.getParameter("idPatient"));
		patient = Rmi.getPatient(idPatient);
		debutRecherche = nbRecherche*numpage;
		manager.setListOrdonnance(Rmi.rechercheOrdonnance(patient.getId(),debutRecherche,nbRecherche));
		if(manager.getListOrdonnance().size()>=nbRecherche) pageSuivant = true;
		else  pageSuivant = false;
		
		if(numpage>0) pagePrecedente = true;
		else pagePrecedente = false;
	}
%>


<%
	ArrayList<String> messages = manager.extraireMessages();
	if(messages.size()>0){
		out.println("<div class=\"notif-erreur\">");
		out.println("<ul>");
		for (int i=0;i < messages.size();i++){
			out.println("<li>" + messages.get(i) + "</li>");
		}
		out.println("</ul>");
		out.println("</div>");
	}
%>



<!-- 
<div><a href="ajouterOrdonnance.jsp">Ajouter une ordonnance</a></div>
 -->
<%
	out.println("<h2>Liste des ordonnances de "+patient.getPrenom()+" "+patient.getNom()+"</h2>");
	if(manager.getListOrdonnance() != null && manager.getListOrdonnance().size()!=0){
		out.println("<div>");
		out.println("<table>");
		
		out.println("<tr>");
		out.println("<td></td>");
		out.println("<td>Date</td>");
		out.println("<td>Medecin</td>");
		out.println("</tr>");
		
		for(int i=0;i<manager.getListOrdonnance().size();i++){
			Ordonnance ordonnance = manager.getListOrdonnance().get(i);
			Medecin medecin = Rmi.getMedecin(ordonnance.getIdMedecin());
			out.println("<tr>");
			out.print("<td><form action=\"consulterOrdonnance.jsp\" method=\"post\">");
			out.print("<input type=\"hidden\" name=\"idOrdonnance\" value=\""+i+"\" />");
			out.println("<input type=\"submit\" name=\"consulterOrdonnance\" value=\"Consulter\" /></form></td>");
			
			out.println("<td>"+ordonnance.getDate()+"</td>");
			out.println("<td>"+medecin.getPrenom()+" "+medecin.getNom()+"</td>");
			out.println("</tr>");
		}
		
		out.print("<tr>");
		if(pagePrecedente){
			out.print("<td>");
			out.print("<form action=\"listerOrdonnance.jsp\" method=\"post\">");
			out.print("<input type=\"hidden\" name=\"idPatient\" value=\""+patient.getId()+"\" />");
			out.print("<input type=\"hidden\" name=\"numPage\" value=\""+(numPage-1)+"\" />");
			out.println("<input type=\"submit\" name=\"changePage\" value=\"Page precedente\" /></form></td>");
		}
		if(pageSuivant){
			out.print("<td>");
			out.print("<form action=\"listerOrdonnance.jsp\" method=\"post\">");
			out.print("<input type=\"hidden\" name=\"idPatient\" value=\""+patient.getId()+"\" />");
			out.print("<input type=\"hidden\" name=\"numPage\" value=\""+(numPage+1)+"\" />");
			out.println("<input type=\"submit\" name=\"changePage\" value=\"Page suivante\" /></form></td>");
		}
		out.print("</tr>");
		
		out.println("</table>");
		out.println("</div>");
	}
%>